package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApBehaviorEntryMapper;
import com.heima.behavior.mapper.ApFollowBehaviorMapper;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApFollowBehaviorService;
import com.heima.model.behavior.dtos.FollowBehaviorDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApFollowBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
@Service
public class ApFollowBehaviorServiceImpl extends ServiceImpl<ApFollowBehaviorMapper, ApFollowBehavior> implements ApFollowBehaviorService {

    /**
     * 存储关注数据
     *
     * @param dto
     * @return
     */
    @Autowired
    ApFollowBehaviorMapper apFollowBehaviorMapper;
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    @Override
    public ResponseResult saveFollowBehavior(FollowBehaviorDto dto) {
        /*1参数检查*/
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        /*2查询关注行为*/
        //TODO:
            /*查找用户注册时产生的实体ID，普通用户注册成功就会在entry表中插入数据*/
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(dto.getUserId(), null);
        if (apBehaviorEntry == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        /*3保存关注行为*/
            /*查找行为实体是否存在;如果存在就不用下一步操作，如果不存在就新建插入*/
        ApFollowBehavior apfb = getOne(Wrappers.<ApFollowBehavior>lambdaQuery()
                .eq(ApFollowBehavior::getEntryId, apBehaviorEntry.getId())
                .eq(ApFollowBehavior::getFollowId, dto.getFollowId()));
        if (apfb == null) {
            ApFollowBehavior apFollowBehavior = new ApFollowBehavior();
            apFollowBehavior.setEntryId(apBehaviorEntry.getId());
            apfb.setCreatedTime(new Date());
            apfb.setArticleId(dto.getArticleId());
            apfb.setFollowId(dto.getFollowId());
            save(apfb);
        }
        /*4返回结果*/
        return null;
    }
}
